1 /** 2 DB Idea Source: 3 http://zetcode.com/db/sqlite/constraints/ 4 */ 5 module test.authors; 6 7 import db_constraints; 8 9 version(unittest) 10 class Author 11 { 12 private: 13 int _AuthorId; 14 string _Name; 15 public: 16 @PrimaryKeyColumn @NotNull 17 @property void AuthorId(int value) 18 { 19 setter(_AuthorId, value); 20 } 21 @UniqueConstraintColumn!"uc_Author" 22 @property string Name() 23 { 24 return _Name; 25 } 26 this(int AuthorId_, string Name_) 27 { 28 this._AuthorId = AuthorId_; 29 this._Name = Name_; 30 initializeKeyedItem(); 31 } 32 Author dup() 33 { 34 return new Author(this._AuthorId, this._Name); 35 } 36 37 override bool opEquals(Object o) const pure nothrow @nogc 38 { 39 auto rhs = cast(immutable Author)o; 40 return (rhs !is null && this.uc_Author_key == rhs.uc_Author_key); 41 } 42 mixin KeyedItem!(); 43 } 44 45 version(unittest) 46 class Authors : BaseKeyedCollection!(Author) 47 { 48 public: 49 this(Author[] items) 50 { 51 super(items); 52 } 53 static Authors GetFromDB() 54 { 55 return new Authors([ 56 new Author(1, "Jane Austen"), 57 new Author(2, "Leo Tolstoy"), 58 new Author(3, "Joseph Heller"), 59 new Author(4, "Charles Dickens") 60 ]); 61 } 62 } 63 64 unittest 65 { 66 auto authors = Authors.GetFromDB(); 67 // using integers since AuthorId is the clustered index 68 for(int i = 0; i < authors.length; ++i) 69 { 70 for(int j = i + 1; j < authors.length; ++j) 71 { 72 assert(authors[i + 1] != authors[j + 1]); 73 } 74 } 75 }